import 'package:chat/common/widgets/Chat.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'index.dart';

class ChatdetailPage extends GetView<ChatdetailController> {
  const ChatdetailPage({super.key});

  // ✅ 主体视图
  Widget _buildView(BuildContext context, ChatdetailController controller) {
    return Column(
      children: [
        /// ✅ 聊天消息列表 + 下拉刷新、上拉加载
        Expanded(
          child: Obx(() {
            return SmartRefresher(
              controller: controller.refreshController,
              enablePullDown: true,
              // ✅ 下拉刷新
              enablePullUp: true,
              // ✅ 上拉加载
              onRefresh: controller.onRefresh,
              onLoading: controller.onLoading,
              // ✅ 最新消息在底部
              reverse: true,
              footer: CustomFooter(
                builder: (context, mode) {
                  if (mode == LoadStatus.noMore) {
                    return const Center(
                      child: Padding(padding: EdgeInsets.all(8.0), child: Text("—— 没有更多消息 ——", style: TextStyle(color: Colors.grey))),
                    );
                  } else if (mode == LoadStatus.loading) {
                    return const Center(child: Padding(padding: EdgeInsets.all(8.0), child: CircularProgressIndicator(strokeWidth: 2)));
                  } else {
                    return const SizedBox.shrink();
                  }
                },
              ),

              child: ListView.builder(
                controller: controller.scrollController,
                padding: const EdgeInsets.only(bottom: 10),
                itemCount: controller.userMessageList.length,
                itemBuilder: (context, index) {
                  final message = controller.userMessageList[index];
                  final isMe = message.senderId == controller.userId.value;
                  return Chat(
                    isMe: isMe,
                    content: message.content,
                    userAvatar: controller.userAvatar.value,
                    friendAvatar: controller.friendAvatar.value,
                  );
                },
              ),
            );
          }),
        ),

        /// ✅ 输入框区域
        _buildInputBar(context, controller),
      ],
    );
  }

  // ✅ 输入框
  Widget _buildInputBar(BuildContext context, ChatdetailController controller) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
      child: SafeArea(
        top: false,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            IconButton(icon: const Icon(Icons.mic_none_rounded), onPressed: () {}),

            /// 输入框
            Expanded(
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 10),
                decoration: BoxDecoration(color: Colors.grey[200], borderRadius: BorderRadius.circular(20)),
                child: TextField(
                  controller: controller.textController,
                  minLines: 1,
                  maxLines: 5,
                  textInputAction: TextInputAction.send,
                  onSubmitted: (_) => controller.sendMessage(),
                  decoration: const InputDecoration(
                    hintText: "请输入消息",
                    border: InputBorder.none,
                    contentPadding: EdgeInsets.symmetric(vertical: 10),
                  ),
                ),
              ),
            ),

            const SizedBox(width: 5),

            /// 发送按钮
            IconButton(icon: const Icon(Icons.send, color: Colors.blueAccent), onPressed: controller.sendMessage),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChatdetailController>(
      init: ChatdetailController(),
      id: "chatdetail",
      builder: (controller) {
        return Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: AppBar(title: Obx(() => Text(controller.friendName.value)), centerTitle: true, backgroundColor: Colors.white, elevation: 0.5),
          body: _buildView(context, controller),
        );
      },
    );
  }
}
